package com.longrise.android.byjk.plugins.vip.integralexchange;

import android.app.Activity;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.event.PersonalInfoEvent;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.local.sp.CacheUtils;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.DZZWTools;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by callxkj on 2017/8/14.
 * 个人资料我的勋章排名的帮助类
 */

public class MyMedalRankHelper {

    private static String rankKey = UserInfor.getInstance().getUserid() + "MEDAL_RANK_KEY";

    /**
     * @return  返回勋章排名的string
     */
    public static String getMedalRank() {
        return CacheUtils.getString(rankKey, "无");
    }

    /**
     * 重新保存勋章的排名
     * @param isShowProgress 是否显示请求网络动画
     */
    public static void reloadRank(final Activity activity, final boolean isShowProgress) {
        EntityBean bean = new EntityBean();
        bean.set("userid", UserInfor.getInstance().getUserid());
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        if (isShowProgress && activity instanceof BaseActivity) {
            DZZWTools.showWaitDialog2(activity);
        }
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_vip_sVIPMedalstate", fbean, new LoadDataManager.OnRequestCallListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            String resultdesc = bean.getString("resultdesc");
                            int value = (Integer) bean.get("resultstate");
                            if (value == 1) {
                                EntityBean[] results = bean.getBeans("result");
                                parseResult(results);
                            } else {
                                CacheUtils.setString(rankKey, "无");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        PersonalInfoEvent event = new PersonalInfoEvent(false);
                        event.setmIsUpdateMyMedal(true);
                        EventBus.getDefault().post(event);//告诉我的勋章页面刷新 勋章排序
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        if(isShowProgress){
                            DZZWTools.dismissDialog(null);
                        }
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {

                    }
                });
    }

    private static void parseResult(EntityBean[] beans) {
        if (beans == null) {
            CacheUtils.setString(rankKey, "无");
            return;
        }
        StringBuilder builder = new StringBuilder();
        for (int x = 0; x < beans.length; x++) {
            EntityBean bean = beans[x];
            String areaname = bean.getString("areaname");
            if (x == beans.length - 1) {
                builder.append(areaname);
            } else {
                builder.append(areaname + "，");
            }
        }
        String s = builder.toString();
        CacheUtils.setString(rankKey, s);
    }

}
